import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * GUI-based widget for converting temperature values from Celsius to Fahrenheit.
 * Slightly more advanced demonstration of Java Swing GUI toolkit. Creates a data
 * entry form with text input, multiple labels, and a button. An action listener
 * calls a method to perform the conversion & display the output when the button
 * is pressed (actually, when <i>any</i> button event is detected.
 * 
 * @author charlieg
 * @author <a href="mailto:charlieg@cis.udel.edu">charlieg@cis.udel.edu</a>
 * @author <a href="http://www.cis.udel.edu/~charlieg">www.cis.udel.edu/~charlieg</a>
 * @version 1.0, &nbsp; 22 Sept 2009
 *
 */
public class CelsiusConverterGUI extends JFrame {

	// variables used throughout class
    private static JLabel celsiusLabel;
    private static JButton convertButton;
    private static JLabel fahrenheitLabel;
    private static JTextField inputTextField;
    
    /**
     * Creates & displays Celsius conversion window.
     */
    public CelsiusConverterGUI() {
        initComponents();
        
        // show the window
        pack();
        setVisible(true);
    }
	
    /**
     * Initializes GUI window by adding components.
     */
    private void initComponents() {
    	
    	// set up the window
        setTitle("Celsius Converter");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(250, 100));

        // set up the individual components
        inputTextField = new JTextField(10);
        celsiusLabel = new JLabel("Celsius");
        convertButton = new JButton("Convert");
        fahrenheitLabel = new JLabel("Fahrenheit");
        
        // initialize window layout & add components
        setLayout(new FlowLayout());
        getContentPane().add(inputTextField);
        getContentPane().add(celsiusLabel);
        getContentPane().add(convertButton);
        getContentPane().add(fahrenheitLabel);
        
        // create & assign action listener for button
        convertButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                convertButtonActionPerformed(evt);
            }
        });
    }
    
    /**
     * Performs temperature conversion when button event is detected.
     * Celsius temperature value entered into text field is converted to Fahrenheit
     * and displayed in Fahrenheit label.
     * 
     * @param evt Button event detected
     */
    private static void convertButtonActionPerformed(ActionEvent evt) {
    	
    	// parse Celsius value as Double, convert to Fahrenheit, cast as int
    	int tempFahr = (int)((Double.parseDouble(inputTextField.getText())) * 1.8 + 32);
    	
    	// change text of Fahrenheit label to reflect converted value
    	fahrenheitLabel.setText(tempFahr + " Fahrenheit");
    }
}